use ferris_says::say;
use std::io::{stdout, BufWriter};

fn types() {
    let arr: [i32; 2] = [3, 5];
    let arr1: [2; 2] = [2, 2];
    let c = [1, 2, 3];

}

fn flow() {
    // 不会报错，但会warning, 变量a不可变，但可以重新绑定-重影shadowing
    let c = 123;
    let c: i32 = 456;

    let a = 123;
    let b = a;
    println!("{a} is!");

    // error
    // const b: i32 = 789;
    // let b = 65;

    if c > 132 {
        println!("c is {}", c); // 456
    }

    // 无限循环
    let mut d = 2;
    loop {
        d += 2;

        if d > 8 {
            println!("");
            break;
        }
        print!("{}", d);
    }

    // while

    // for
    for number in 2..5 {
        print!("{}!", number) // 2-4, 不包含5
    }
}

fn ownership() {
    let s1 = String::from("string len()");
    let s2 = &s1;
    // let mut s3 = String::from("456");
    // let s4 = &mut s3;
    // let s5 = &mut s3; // error mut references不可同时borrowing
    let len = calculate_length(&s1); // 防止s1所有权转移到函数内部

    // println!("{}, {}", s4, s5);
    println!("s2 is {s2}");
    println!("The length of '{s1}' is {len}.");
}

fn calculate_length(s: &String) -> usize {
    s.len() // 返回
}

fn main() {
    flow();

    types();

    ownership();

    let stdout = stdout();
    let message = String::from("Hello fellow Rustaceans!");
    let width = message.chars().count();

    let mut wirter = BufWriter::new(stdout.lock());
    say(&message, width, &mut wirter).unwrap();

    print!("hello {{ }} {0} {1}", "rust", "rust1"); // {{ }}, 输出{ }
}